function WP_CheckStep(line)
	_, class = UnitClass("player")
	_, race = UnitRace("player")
	
	class = string.lower(class)
	race = string.lower(race)
	
	if race == "scourge" then race = "undead" end
	
	local anyclass = false
	local anycheck = false
	repeat
		accclass, line, checked = WP_CheckClass(line, class)
		
		if checked then
			if accclass then anyclass = true end
			anycheck = true
		end
	until checked == false
	if anycheck then
		if anyclass == false then return false end
	end

	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckClassNot(line, class)
		
		if checked == true and accclass == false then return false end
	until checked == false

	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckRace(line, race)
		
		if checked then
			if accclass then anyclass = true end
			anycheck = true
		end
	until checked == false
	if anycheck then
		if anyclass == false then return false end
	end

	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckRaceNot(line, race)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckLevel(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckBound(line)
		
		if checked == true and accclass == false then return false end
	until checked == false

	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckNotBound(line)
		
		if checked == true and accclass == false then return false end
	until checked == false

	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckTaxiKnown(line)
		
		if checked == true and accclass == false then return false end
	until checked == false

	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckTaxiNotKnown(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckFactionRep(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckFactionRepNot(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckNeedRepair(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckNeedNotRepair(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckQuest(line)
		
		if checked then
			if accclass then anyclass = true end
			anycheck = true
		end
	until checked == false
	if anycheck then
		if anyclass == false then return false end
	end
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckNotZone(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckZone(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckOnQuest(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckNotOnQuest(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckHaveSkill(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	anyclass, anycheck = false, false
	repeat
		accclass, line, checked = WP_CheckNotHaveSkill(line)
		
		if checked == true and accclass == false then return false end
	until checked == false
	
	return true
end 


function WP_CheckClass(line, class)
	local f1 = nil

	_, _, f1 = string.find(line, "<checkClass=(.-)>")
	if f1 then
		f1 = string.lower(f1)
		WP_Debug(WP_debugClass, "Comparing class '"..class.."' with class '"..f1.."' for match")
		line  = string.gsub(line, "<checkClass=.->", "", 1)
	
		if f1 == class then return true, line, true end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckClassNot(line, class)
	local f1 = nil

	_, _, f1 = string.find(line, "<checkNotClass=(.-)>")
	if f1 then
		f1 = string.lower(f1)
		WP_Debug(WP_debugClass, "Comparing class '"..class.."' with class '"..f1.."' for not matching")
		line  = string.gsub(line, "<checkNotClass=.->", "", 1)
	
		if f1 ~= class then return true, line, true end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckRace(line, race)
	local f1 = nil
	
	_, _, f1 = string.find(line, "<checkRace=(.-)>")
	if f1 then
		f1 = string.lower(f1)
		WP_Debug(WP_debugRace, "Comparing race '"..race.."' with race '"..f1.."' for match")
		line = string.gsub(line, "<checkRace=.->", "", 1)
		
		if f1 == race then return true, line, true end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckRaceNot(line, race)
	local f1 = nil
	
	_, _, f1 = string.find(line, "<checkNotRace=(.-)>")
	if f1 then
		f1 = string.lower(f1)
		WP_Debug(WP_debugRace, "Comparing race '"..race.."' with race '"..f1.."' for not matching")
		line = string.gsub(line, "<checkNotRace=.->", "", 1)
		
		if f1 ~= race then return true, line, true end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckLevel(line)
	local f1,f2 = nil
	local lvl = UnitLevel("player")
	local curXP = UnitXP("player")
	local maxXP = UnitXPMax("player")

	_, _, f3 = string.find(line, "<checkLevel=(.-)>")
	if f3 then
		_, _, f1, f2 = string.find(f3, "(%d+)=%-(%d+)")
		if f1 and f2 then
			f1 = f1 + 0
			f2 = f2 + 0
			line = string.gsub(line, "<checkLevel="..f1.."=%-"..f2..">", "", 1)
			local reqLvl = f1 - 1
			local reqXP = maxXP - f2
			
			if (lvl == reqLvl and curXP >= reqXP) or (lvl > reqLvl) then
				return false, line, true
			end
			
			return true, line, true
		end

		f1, f2 = nil, nil
		_, _, f1, f2 = string.find(f3, "(%d+)=(%d+)")
		if f1 and f2 then
			f1 = f1 + 0
			f2 = f2 + 0
			line = string.gsub(line, "<checkLevel="..f1.."="..f2..">", "", 1)
			if (lvl == f1 and curXP >= f2) or (lvl > f1) then
				return false, line, true
			end
			
			return true, line, true
		end
	
		f3 = f3 + 0
		line = string.gsub(line, "<checkLevel="..f3..">", "", 1)
		
		if lvl >= f3 then return false, line, true end
		
		return true, line, true
	end
	
	return true, line, false
end


function WP_CheckBound(line)
	_, _, f1 = string.find(line, "<checkInn=(.-)>")
	if f1 then
		cur = GetBindLocation()
		line = string.gsub(line, "<checkInn=.->", "", 1)
		
		if cur == f1 then return true, line, true end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckNotBound(line)
	_, _, f1 = string.find(line, "<checkNotInn=(.-)>")
	if f1 then
		cur = GetBindLocation()
		line = string.gsub(line, "<checkNotInn=.->", "", 1)
		
		if cur == f1 then return false, line, true end
		
		return true, line, true
	end
	
	return true, line, false
end


function WP_CheckTaxiKnown(line)
	_, _, f1 = string.find(line, "<checkFlight=(.-)>")
	if f1 then
		line = string.gsub(line, "<checkFlight=.->", "", 1)
		
		local numNodes = NumTaxiNodes()
		for slot = 1, numNodes do
			local nodeName = TaxiNodeName(slot)
			
			_, _, name = string.find(nodeName, "(.+),.+")
			if not name then name = nodeName end
			
			if name == f1 then return true, line, true end
		end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckTaxiNotKnown(line)
	_, _, f1 = string.find(line, "<checkNotFlight=(.-)>")
	if f1 then
		line = string.gsub(line, "<checkNotFlight=.->", "", 1)
		
		local numNodes = NumTaxiNodes()
		for slot = 1, numNodes do
			local nodeName = TaxiNodeName(slot)
			
			_, _, name = string.find(nodeName, "(.+),.+")
			if not name then name = nodeName end
			
			if name == f1 then return false, line, true end
		end
		
		return true, line, true
	end
	
	return true, line, false
end


function WP_CheckFactionRep(line)
	local checked = false
	local accept = true
	
	checked = false
	accept = true
	
	_, _, f1, f2 = string.find(line, "<checkRep=(.-)=(%d-)>")
	if f1 and f2 then
		line = string.gsub(line, "<checkRep=.-=.->", "", 1)
		f2 = f2 + 0
		
		local collapsedFactions = {}
		local numFactions = GetNumFactions()
		local prevNumFactions = numFactions
		local factionExpanded = false
		
		repeat
			factionExpanded = false
			prevNumFactions = numFactions
			for i = 1,numFactions do
				if not factionExpanded then
					name, description, standingId, bottomValue, topValue, earnedValue, atWarWith,
						canToggleAtWar, isHeader, isCollapsed, hasRept, isWatched, isChild = GetFactionInfo(i)
					
					if isCollapsed then
						table.insert(collapsedFactions, 1, i)
						ExpandFactionHeader(i)
						factionExpanded = true
					end
				end
			end
			numFactions = GetNumFactions()
		until numFactions == prevNumFactions
		
		for i = 1,numFactions do
			name, description, standingId, bottomValue, topValue, earnedValue, atWarWith,
				canToggleAtWar, isHeader, isCollapsed, hasRept, isWatched, isChild = GetFactionInfo(i)
			
			if f1 == name then
				checked = true
				
				if earnedValue >= f2 then
					accept = true
				else
					accept = false
				end
			end
		end
		
		for k,v in pairs(collapsedFactions) do
			CollapseFactionHeader(v)
		end
	end
	
	return accept, line, checked
end

function WP_CheckFactionRepNot(line)
	local checked = false
	local accept = true
	
	checked = false
	accept = true
	
	_, _, f1, f2 = string.find(line, "<checkNotRep=(.-)=(%d-)>")
	if f1 and f2 then
		line = string.gsub(line, "<checkNotRep=.-=.->", "", 1)
		f2 = f2 + 0
		
		local collapsedFactions = {}
		local numFactions = GetNumFactions()
		local prevNumFactions = numFactions
		local factionExpanded = false
		
		repeat
			factionExpanded = false
			prevNumFactions = numFactions
			for i = 1,numFactions do
				if not factionExpanded then
					name, description, standingId, bottomValue, topValue, earnedValue, atWarWith,
						canToggleAtWar, isHeader, isCollapsed, hasRept, isWatched, isChild = GetFactionInfo(i)
					
					if isCollapsed then
						table.insert(collapsedFactions, 1, i)
						ExpandFactionHeader(i)
						factionExpanded = true
					end
				end
			end
			numFactions = GetNumFactions()
		until numFactions == prevNumFactions
		
		for i = 1,numFactions do
			name, description, standingId, bottomValue, topValue, earnedValue, atWarWith,
				canToggleAtWar, isHeader, isCollapsed, hasRept, isWatched, isChild = GetFactionInfo(i)
			
			if f1 == name then
				checked = true
				
				if earnedValue < f2 then
					accept = true
				else
					accept = false
				end
			end
		end
		
		for k,v in pairs(collapsedFactions) do
			CollapseFactionHeader(v)
		end
	end
	
	return accept, line, checked
end


function WP_CheckNeedRepair(line)
	if string.find(line, "<checkRepair>") then
		WP_Debug(WP_debugRepair, "Check if we need to repair")
		line = string.gsub(line, "<checkRepair>", "", 1)
		
		local needRepair = false
		
		local slotNames = {"Head", "Shoulder", "Chest", "Wrist", "Hands", "Waist", "Legs", "Feet",
			"MainHand", "SecondaryHand", "Ranged"}
		
		for k,v in pairs(slotNames) do
			slotId = GetInventorySlotInfo(v.."Slot")
			
			if (GetInventoryItemTexture("player", slotId)) then
				local dur, maxDur = GetInventoryItemDurability(slotId)
				if dur < maxDur then needRepair = true end
			end
		end
		
		return needRepair, line, true
	end
	
	return true, line, false
end


function WP_CheckNeedNotRepair(line)
	if string.find(line, "<checkNotRepair>") then
		WP_Debug(WP_debugRepair, "Check if we do not need to repair")
		line = string.gsub(line, "<checkNotRepair>", "", 1)
		
		local needRepair = true
		
		local slotNames = {"Head", "Shoulder", "Chest", "Wrist", "Hands", "Waist", "Legs", "Feet",
			"MainHand", "SecondaryHand", "Ranged"}
		
		for k,v in pairs(slotNames) do
			slotId = GetInventorySlotInfo(v.."Slot")
			
			if (GetInventoryItemTexture("player", slotId)) then
				local dur, maxDur = GetInventoryItemDurability(slotId)
				if dur < maxDur then needRepair = false end
			end
		end
		
		return needRepair, line, true
	end
	
	return true, line, false
end


function WP_CheckQuest(line)
	_, _, f1 = string.find(line, "<checkQuest=(.-)>")
	
	if f1 then
		local numEntries = GetNumQuestLogEntries()
		line = string.gsub(line, "<checkQuest=.->", "", 1)

		_, _, f2, f3 = string.find(f1, "(.+)=(%d+)")
		if f2 and f3 then
			f3 = f3 + 0
			for i = 1, numEntries, 1 do
				local title = GetQuestLogTitle(i)
				if title == f2 then
					local _,_, done = GetQuestLogLeaderBoard(f3,i)
					if done then  return false, line, true end
				end

				return true, line, true
			end
		else
			for i = 1, numEntries, 1 do
				local title,_,_,_,_,_,complete = GetQuestLogTitle(i)		
				if complete and title == f1 then return false, line, true end
			end

			return true, line, true
		end
	end
	
	return true, line, false
end


function WP_CheckOnQuest(line)
	_, _, f1 = string.find(line, "<checkOnQuest=(.-)>")
	
	if f1 then
		local numEntries = GetNumQuestLogEntries()
		line = string.gsub(line, "<checkOnQuest=.->", "", 1)

		for i = 1, numEntries, 1 do
			local title = GetQuestLogTitle(i)		
			if title == f1 then return true, line, true end
		end
	
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckNotOnQuest(line)
	_, _, f1 = string.find(line, "<checkNotOnQuest=(.-)>")
	
	if f1 then
		local numEntries = GetNumQuestLogEntries()
		line = string.gsub(line, "<checkNotOnQuest=.->", "", 1)

		for i = 1, numEntries, 1 do
			local title = GetQuestLogTitle(i)		
			if title == f1 then return false, line, true end
		end

		return true, line, true
	end
	
	return true, line, false
end


function WP_CheckZone(line)
	_, _, f1 = string.find(line, "<checkZone=(.-)>")
	
	if f1 then
		line = string.gsub(line, "<checkZone=.->", "", 1)

		local zone = GetZoneText()
		local subzone = GetSubZoneText()
		if subzone == "" then subzone = zone end
		local fullzone = zone .. ":" .. subzone
		
		if f1 == zone or f1 == subzone or f1 == fullzone then return true, line, true end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckNotZone(line)
	_, _, f1 = string.find(line, "<checkNotZone=(.-)>")
	
	if f1 then
		line = string.gsub(line, "<checkZone=.->", "", 1)

		local zone = GetZoneText()
		local subzone = GetSubZoneText()
		if subzone == "" then subzone = zone end
		local fullzone = zone .. ":" .. subzone
		
		if f1 == zone or f1 == subzone or f1 == fullzone then return false, line, true end
		
		return true, line, true
	end
	
	return true, line, false
end


function WP_CheckHaveSkill(line)
	_, _, f1 = string.find(line, "<checkSkill=(.-)>")
	
	if f1 then
		line = string.gsub(line, "<checkSkill=.->", "", 1)
		
		_, _, f2, f3 = string.find(f1, "(%d+)=(%d+)")
		if f2 and f3 then
			f1 = f2
			f2 = f3
			f3 = nil
		end
		
		numTabs = GetNumSpellTabs()
		for i = 1,numTabs do
			_, _, offset, numSpells = GetSpellTabInfo(i)
			
			for j = 1,numSpells do
				name, rank = GetSpellInfo(offset + j)
				
				if name == f1 then
					if f2 and f2 >= rank then
						return true, line, true
					else
						return false, line, true
					end
					
					return true, line, true
				end
			end
		end
		
		return false, line, true
	end
	
	return true, line, false
end


function WP_CheckNotHaveSkill(line)
	_, _, f1 = string.find(line, "<checkNotSkill=(.-)>")
	
	if f1 then
		line = string.gsub(line, "<checkNotSkill=.->", "", 1)
		
		_, _, f2, f3 = string.find(f1, "(%d+)=(%d+)")
		if f2 and f3 then
			f1 = f2
			f2 = f3
			f3 = nil
		end
		
		numTabs = GetNumSpellTabs()
		for i = 1,numTabs do
			_, _, offset, numSpells = GetSpellTabInfo(i)
			
			for j = 1,numSpells do
				name, rank = GetSpellInfo(offset + j)
				
				if name == f1 then
					if f2 and f2 >= rank then
						return false, line, true
					else
						return true, line, true
					end
					
					return false, line, true
				end
			end
		end
		
		return true, line, true
	end
	
	return true, line, false
end
